<template>
    <div style="position: relative;">
        <!-- 面包屑导航 -->
        <el-breadcrumb separator-class="el-icon-arrow-right" class="tit">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>{{ title }}</el-breadcrumb-item>
        </el-breadcrumb>
        <!-- 添加按钮 -->
        <el-button @click="drawer = true" type="primary">添加用户</el-button>
        <!-- 添加展开 -->
        <el-drawer title="添加用户" :visible.sync="drawer" :direction="direction" :before-close="handleClose">
            <el-form ref="userref" :model="adduser" label-width="80px" :rules="rules">
                <el-form-item label="用户名" prop="username">
                    <el-input v-model="adduser.username"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                    <el-input v-model="adduser.password" show-password></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="adduser.mobile"></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="adduser.email"></el-input>
                </el-form-item>
            </el-form>
            <el-button @click="adduserfun" type="primary" style="display: block;margin: 12px auto 0;">添加</el-button>
        </el-drawer>
        <!-- tabel表格 -->
        <el-table :data="userList" style="width: 100%">
            <el-table-column prop="create_time" label="日期" width="130">
            </el-table-column>
            <el-table-column prop="username" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="id" label="ID" width="70">
            </el-table-column>
            <el-table-column prop="role_name" label="职责" width="200">
            </el-table-column>
            <el-table-column prop="email" label="邮箱">
            </el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button size="mini" @click="handleEdit(scope.row.id)">编辑</el-button>
                    <el-button size="mini" type="danger" @click="handleDelete(scope.$index, scope.row)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页器 -->
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
            :current-page="page.pagenum" :page-sizes="[5, 10, 15, 20]" :page-size="page.pagesize"
            layout="total, sizes, prev, pager, next, jumper" :total="total">
        </el-pagination>
        <!-- 编辑弹出 -->
        <el-dialog title="提示" :visible.sync="dialogVisible" width="40%" :before-close="handleClose">
            <el-form ref="usereditref" :model="edituserlist" label-width="80px">
                <el-form-item label="用户名">
                    <el-input v-model="edituserlist.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="邮箱">
                    <el-input v-model="edituserlist.email"></el-input>
                </el-form-item>
                <el-form-item label="手机号">
                    <el-input v-model="edituserlist.mobile"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="edityes">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>
<script>
import { AddUser, Userslist, GetOneUser } from '@/api/user'
export default {
    data() {
        return {
            userList: null,
            // 用户列表总条数
            total: null,
            // 添加user的对象
            adduser: {},
            edituser: null,
            page: {
                pagenum: 1,
                pagesize: 10
            },
            //控制按钮
            drawer: false,
            direction: 'rtl',
            userlist: {},
            // 约束规则
            rules: {
                username: [
                    { required: true, message: '请输入用户名', trigger: 'blur' },
                ],
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }
                ]
            },
            // 编辑弹出数据
            // 开关
            dialogVisible: false,
            edituserlist: {},
            title: ''
        }
    },
    methods: {
        // 获取用户列表
        async getdata() {
            let res = await Userslist(this.page)
            console.log(res.data.data, '-----------');
            this.userList = res.data.data.users
            this.total = res.data.data.total
        },
        handleClose(done) {
            this.$confirm('确认关闭？')
                .then(_ => {
                    done();
                })
                .catch(_ => { });
        },
        async adduserfun() {
            let { data } = await AddUser(this.adduser)
            console.log(data.data, data.meta);
            if (data.meta.status != 201) return this.$message.error(data.meta.msg);
            this.$message({ message: data.meta.msg, type: 'success' })
            // 清空表单
            this.adduser = {}
            // 关闭对话框
            this.drawer = false
            // 重新渲染userlist列表
            this.getdata()
        },
        // 分页器函数
        handleSizeChange(val) {
            this.page.pagenum = 1;
            this.page.pagesize = val
            this.getdata()
        },
        handleCurrentChange(val) {
            this.page.pagenum = val
            this.getdata()
        },
        // 操作函数
        async handleEdit(row) {//编辑按钮
            this.dialogVisible = true;
            console.log(row);
            let { data } = await GetOneUser(row)
            console.log(data);
            this.edituserlist = data.data;

        },
        // 确定编辑按钮
        edityes() {
            // this.dialogVisible = false
        },
        handleDelete(index, row) {
            console.log(index, row);
        }
    },
    mounted() {
        this.getdata()
        this.title=sessionStorage.getItem('title')
    }
}
</script>
<style>
.el-pagination {
    margin-top: 15px;
}
.tit{
    position: absolute;
    left: 5px;
    top: -18px;
}
</style>